from __future__ import annotations

from typing import List

from .models import Question, KnowledgePoint, QualityResult


class QualityValidator:
    def validate(self, question: Question, knowledge_point: KnowledgePoint) -> QualityResult:
        messages: List[str] = []
        score = 1.0

        if not question.stem or len(question.stem.strip()) < 6:
            messages.append("题干过短或为空")
            score -= 0.2

        if knowledge_point.name not in question.stem:
            messages.append("题干未显式包含知识点名（可选要求）")
            score -= 0.05

        if question.type == "选择题":
            if not question.options or len(question.options) < 3:
                messages.append("选项数量不足")
                score -= 0.2
            if question.correct_answer is None:
                messages.append("缺少正确答案")
                score -= 0.3
        if question.type == "判断题":
            if question.options is None or set(question.options) != {"正确", "错误"}:
                messages.append("判断题选项应为正确/错误")
                score -= 0.1

        if question.difficulty < 1 or question.difficulty > 10:
            messages.append("难度越界")
            score -= 0.2

        score = max(0.0, min(1.0, score))
        passed = score >= 0.6
        return QualityResult(passed=passed, score=score, messages=messages)